/** @jsxImportSource @emotion/react */
import * as React from "react";
import {  OriginalViewProps,  } from "../../common/base";
import {
  ResultDiagram, EvaluationForm, config测量仪, EntranceSetup,
} from "./orcBase";
import {createItem, EditorAreaConfig, } from "../../common/eHelper";
import {useRecordListSubX} from "../../hook/useRecordListSub";
import {EditStorageContext} from "../../StorageContext";
import {TestMethod} from "../radio/editor";
import {Button} from "customize-easy-ui-component";

/**给config测量仪的编辑字段提供默认赋值；   原本默认=undefined;
 * 【注意】只有打开编辑器确认才有生效的；还有问题，这么处理后，若需要真正的改成为空的，必须录入空格' '字符串才能成事，否则还是默认值。适配字符型的存储字段。
 * */
const defvcbFunc = (par: any) => {
  // if(!par.检标准)  par.检标准='GB/T 16579-2019';
  if(!par.仪器名)  par.仪器名='里氏硬度计';
  return  par;
};


//用了TestMethod缺点：没法向Instrument页面去添加一个独立的编辑字段。
const recordPrintList = [
    createItem('Instrument', <TestMethod config={config测量仪}  defValCb={defvcbFunc} label={'耐压试验报告-概况'}>
          </TestMethod>),
    createItem('EvaluationForm', <EvaluationForm/>),
    createItem('ResultDiagram', <ResultDiagram/>),
] as EditorAreaConfig[];
if(process.env.REACT_APP_TEST==='true')  recordPrintList.splice(0,0,createItem('Entrance', <EntranceSetup/>));



export const OriginalView=
    React.forwardRef((
        { action: modAction,  verId, repId='', inp, rep}
            :OriginalViewProps, ref
    ) => {
      const {storage, setStorage} =React.useContext(EditStorageContext) as any;
      const { _PRSRE_TS: SubRepIds }=storage;
      if(undefined===SubRepIds)
         return <div>请先确定网络和服务器是正常的。然后才考虑是否要做：
                      <Button intent='danger' onPress={() => setStorage({...storage, _PRSRE_TS: [] }) }>
                      还没有数据！立刻初始化</Button></div>;
      //再一次确认，不能去掉：：避免hook报错, 分项报告模板的：【有可能】出现错，否则也没必要拆分两个组件做嵌套
      return <OriginalViewInner ref={ref} action={modAction} verId={verId} repId={repId} rep={rep}/>
} );

/**为了解决useProjectListAs({count: refCount } )因为变动{前面storage数据{有PRSRE_TS没有THICK_MS}和后面storage{有THICK_MS}数据不一样的}，导致hook报错！
 * 只好拆分成2层次嵌套拼装的俩组件了。父组件可以逻辑return子组件可用hook，不违反hook规则不报错。
 * */
const OriginalViewInner=
    React.forwardRef((
        { action: modAction,  verId, repId='', rep}
            :OriginalViewProps, ref
    ) => {
        const titleRender = (store: any) => {
            const firstTag=store?.单内编;
            return <> { `[${firstTag}] `}
            </>;
        }
      const {list}=useRecordListSubX(ref,rep,repId,recordPrintList,modAction,verId,'PRSRE_TS',titleRender);
      return <React.Fragment>
        {list}
      </React.Fragment>;
} );

